package com.das.mechanic_base.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.text.InputType;
import android.util.Log;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3RouterCenter;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class X3Utils {

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 500;
    private static final int PIC_MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;
    private static long changeModelastClickTime;
    private static long changeModelastClickTime2;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    /**
     * 快速点击 防双击
     * @return
     */
    public static boolean isFastClick2() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) <= PIC_MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    /**
     * 快速点击 防双击
     * @return
     */
    public static boolean isFastClick3() {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - changeModelastClickTime) <= 800) {
            Log.e("click_flag","点击太快了");
           return true;
        }
        Log.e("click_flag","正常点击");
        changeModelastClickTime = curClickTime;
        return false;
    }

    /**
     * 快速点击 防双击
     * @return
     */
    public static boolean isFastClick4() {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - changeModelastClickTime2) <= 800) {
            Log.e("click_flag","点击太快了");
            return true;
        }
        Log.e("click_flag","正常点击");
        changeModelastClickTime2 = curClickTime;
        return false;
    }

    /**
     * 跳转到显示图片和视频
     * @param mContext
     * @param imgList
     * @param index
     */
    public static void startShowImage(Context mContext, List<String> imgList, int index) {
        XXPermissions.with(mContext)
                .permission(Permission.Group.STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if(all){
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("imageList", (Serializable) imgList);
                            bundle.putInt("index",index);
                            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.SHOW_IMAGE,bundle);

//                            Intent intent = new Intent(mContext, ShowImageActivity.class);
//                            intent.putExtra("imageList", (Serializable) imgList);
//                            intent.putExtra("index",index);
//                            mContext.startActivity(intent);
                        }else {
                            XXPermissions.startPermissionActivity(mContext,permissions);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        XXPermissions.startPermissionActivity(mContext,permissions);
                    }
                });

    }


    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public static void showInput(EditText et,Context mContext) {
        et.setCursorVisible(true);
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();

        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    public static void hideKeyboard(IBinder token,Context mContext) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 禁掉系统软键盘
     */
    public static void hideSoftInputMethod(Context mContext,EditText mEdit) {
        ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        String methodName = null;
        if (currentVersion >= 16) {
            // 4.2
            methodName = "setShowSoftInputOnFocus";
        } else if (currentVersion >= 14) {
            // 4.0
            methodName = "setSoftInputShownOnFocus";
        }
        if (methodName == null) {
            mEdit.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus;
            try {
                setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(mEdit, false);
            } catch (NoSuchMethodException e) {
                mEdit.setInputType(InputType.TYPE_NULL);
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
